<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="参数值" prop="paramValue">
        <el-input v-model="queryParams.paramValue" placeholder="请输入参数值" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="参数名" prop="paramLabel">
        <el-input v-model="queryParams.paramLabel" placeholder="请输入参数名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="设备类型" prop="deviceTypeId">
        <el-select v-model="queryParams.deviceTypeId" clearable @change="deviceTypeChange">
          <el-option v-for="dict in deviceTypeOptions" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="设备型号" prop="deviceModelId">
        <el-select v-model="queryParams.deviceModelId" clearable>
          <el-option v-for="dict in deviceModelOptions" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:commandParams:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:commandParams:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:commandParams:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:commandParams:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="commandParamsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID主键" align="center" prop="paramsId" v-if="true" />
      <el-table-column label="参数值" align="center" prop="paramValue" />
      <el-table-column label="参数名" align="center" prop="paramLabel" />
      <el-table-column label="参数名(中文)" align="center" prop="paramLabelZhCn" />
      <el-table-column label="参数名" align="center" prop="paramLabelEnUs" />
      <el-table-column label="指令类型" align="center" prop="commandType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.device_param_type" :value="scope.row.commandType" />
        </template>
      </el-table-column>
      <el-table-column label="设备类型" align="center" prop="deviceTypeName"/>
      <el-table-column label="设备型号" align="center" prop="deviceModelName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:commandParams:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:commandParams:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改参数信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="参数值" prop="paramValue">
          <el-input v-model.number="form.paramValue" placeholder="请输入参数值" />
        </el-form-item>
        <el-form-item label="参数名" prop="paramLabel">
          <el-input v-model="form.paramLabel" placeholder="请输入参数名" />
        </el-form-item>
        <el-form-item label="参数名(中文)" prop="paramLabelZhCn">
          <el-input v-model="form.paramLabelZhCn" placeholder="请输入参数名(中文)" />
        </el-form-item>
        <el-form-item label="参数名" prop="paramLabelEnUs">
          <el-input v-model="form.paramLabelEnUs" placeholder="请输入参数名" />
        </el-form-item>
        <el-form-item label="设备类型" prop="deviceTypeId">
          <el-select v-model="form.deviceTypeId" clearable @change="deviceTypeChange">
            <el-option v-for="dict in deviceTypeOptions" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="设备型号" prop="deviceModelId">
          <el-select v-model="form.deviceModelId" clearable>
            <el-option v-for="dict in deviceModelOptions" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="指令类型" prop="commandType">
          <el-select v-model="form.commandType" placeholder="请选择指令类型">
            <el-option v-for="dict in dict.type.device_param_type" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="默认值" prop="paramDefault">
          <el-input v-model="form.paramDefault" placeholder="请输入默认值" />
        </el-form-item>
        <el-form-item label="参数阈值" prop="limitReg">
          <el-input v-model="form.limitReg" placeholder="请输入参数阈值 - 正则表达式" />
        </el-form-item>
        <el-form-item label="参数阈值(中文)" prop="limitRegZhCn">
          <el-input v-model="form.limitRegZhCn" placeholder="请输入参数阈值 - 正则表达式" />
        </el-form-item>
        <el-form-item label="参数阈值(英文)" prop="limitRegEnUs">
          <el-input v-model="form.limitRegEnUs" placeholder="请输入参数阈值 - 正则表达式" />
        </el-form-item>
        <el-form-item label="参数输入类型" prop="paramInput">
          <el-select v-model="form.paramInput" placeholder="请选择参数输入类型">
            <el-option label="数字" :value="1"></el-option>
            <el-option label="文本" :value="2"></el-option>
            <el-option label="下拉" :value="3"></el-option>
            <el-option label="必须执行" :value="4"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.paramInput == 1" label="小数位" prop="paramDecimals">
          <el-input v-model="form.paramDecimals" placeholder="请输入小数位，数字才有" />
        </el-form-item>
        <el-form-item v-if="form.paramInput == 1" label="精度" prop="paramAccuracy">
          <el-input v-model="form.paramAccuracy" placeholder="请输入精度" />
        </el-form-item>
        <el-form-item v-if="form.paramInput == 1" label="单位" prop="paramUnit">
          <el-input v-model="form.paramUnit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="备注-中文" prop="remarkZhCn">
          <el-input v-model="form.remarkZhCn" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="备注-英文" prop="remarkEnUs">
          <el-input v-model="form.remarkEnUs" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { typeTree, getType } from "@/api/device/type";
import { modelTree } from "@/api/device/model";
import { listCommandParams, getCommandParams, delCommandParams, addCommandParams, updateCommandParams } from "@/api/parameter/item/data";
export default {
  name: "CommandParams",
  dicts: ['device_param_type'],
  data() {
    return {
      deviceModelOptions: [],
      deviceTypeOptions: [],
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数信息表格数据
      commandParamsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        paramValue: undefined,
        paramLabel: undefined,
        paramLabelZhCn: undefined,
        paramLabelEnUs: undefined,
        commandType: undefined,
        deviceTypeId: undefined,
        deviceModelId: undefined,
        limitReg: undefined,
        paramInput: undefined,
        paramDecimals: undefined,
        paramAccuracy: undefined,
        paramUnit: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        paramsId: [
          { required: true, message: "ID主键不能为空", trigger: "blur" }
        ],
        paramValue: [
          { required: true, message: "参数值不能为空", trigger: "blur" }
        ],
        paramLabel: [
          { required: true, message: "参数名不能为空", trigger: "blur" }
        ],
        paramLabelZhCn: [
          { required: true, message: "参数名(中文)不能为空", trigger: "blur" }
        ],
        paramLabelEnUs: [
          { required: true, message: "参数名不能为空", trigger: "blur" }
        ],
        commandType: [
          { required: true, message: "指令类型 1参数查询,2参数下发不能为空", trigger: "change" }
        ],
        deviceTypeId: [
          { required: true, message: "设备类型ID不能为空", trigger: "blur" }
        ],
        deviceModelId: [
          { required: true, message: "设备型号ID不能为空", trigger: "blur" }
        ],
        limitReg: [
          { required: true, message: "参数阈值 - 正则表达式(统一阈值不考虑厂商不同时阈值不同)不能为空", trigger: "blur" }
        ],
        paramInput: [
          { required: true, message: "参数输入类型 1、Number为数字，2、Text为文本，3、Select为下拉选项，空则无需输入，4、必须执行不能为空", trigger: "blur" }
        ],
        paramDecimals: [
          { required: true, message: "小数位，数字才有不能为空", trigger: "blur" }
        ],
        paramAccuracy: [
          { required: true, message: "精度不能为空", trigger: "blur" }
        ],
        paramUnit: [
          { required: true, message: "单位不能为空", trigger: "blur" }
        ],
        remark: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getTypeTree();
  },
  methods: {
    /** 查询参数信息列表 */
    getList() {
      this.loading = true;
      listCommandParams(this.queryParams).then(response => {
        this.commandParamsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    getTypeTree() {
      typeTree().then(response => {
        this.deviceTypeOptions = response.data;
      });
    },
    deviceTypeChange(typeId) {
      if (typeId) {
        this.getModelTree(typeId)
      } else {
        this.uploadForm.deviceModelId = undefined
        this.deviceModelOptions = []
      }
    },
    getModelTree(typeId) {
      modelTree(typeId).then(response => {
        this.deviceModelOptions = response.data;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        paramsId: undefined,
        paramValue: undefined,
        paramLabel: undefined,
        paramLabelZhCn: undefined,
        paramLabelEnUs: undefined,
        commandType: undefined,
        deviceTypeId: undefined,
        deviceModelId: undefined,
        limitReg: undefined,
        paramInput: undefined,
        paramDecimals: undefined,
        paramAccuracy: undefined,
        paramUnit: undefined,
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.paramsId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加参数信息";
      this.getTypeTree()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const paramsId = row.paramsId || this.ids
      getCommandParams(paramsId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改参数信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.paramsId != null) {
            updateCommandParams(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addCommandParams(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const paramsIds = row.paramsId || this.ids;
      this.$modal.confirm('是否确认删除参数信息编号为"' + paramsIds + '"的数据项？').then(() => {
        this.loading = true;
        return delCommandParams(paramsIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/commandParams/export', {
        ...this.queryParams
      }, `commandParams_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
